﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.Text.RegularExpressions;

namespace YouHP.Pages.User
{
    public class PostCard : UserBasePage
    {

        protected UserInfo _User = new UserInfo();
        protected PostCardInfo _PostCard = new PostCardInfo();
        protected PatientRecordInfo PrData;
        protected string diseasename;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (RequestTools.IsPost()) HandlePost();
            else HandleGet();
        }

        protected void HandleCommon()
        {
            this.SetTitle("优医网新年明信片邮寄活动！！！");
            this.AddScriptSrc("/support/Alpha/Js/mootools.js");
        }

        protected void HandleGet()
        {
            _User = UserBLL.Instance.GetUserInfo(UserData.Id);
            PrData = PatientBLL.Instance.GetPatientRecordInfo(_User.Id);
            diseasename = DiseaseBLL.Instance.GetDiseaseInfo(PrData.DiseaseId).Name;

        }

        protected void HandlePost()
        {

            _PostCard.UserId = UserData.Id;
            _PostCard.UserName = RequestTools.GetString("username");
            _PostCard.TrueName = RequestTools.GetString("truename");
            if (_PostCard.TrueName.Trim().Length < 2)
            {
                this.AddErrLine("您的真实姓名未填或不正确，我们将无法向您寄送明信片！！");
                return;
            }

            _PostCard.Avatar = RequestTools.GetString("new_avatar");

            _PostCard.Email = RequestTools.GetString("email");

            _PostCard.MSN = RequestTools.GetString("msn");
            _PostCard.QQ = RequestTools.GetString("qq");
            _PostCard.Phone = RequestTools.GetString("mobile");
            
            _PostCard.DiseaseName = RequestTools.GetString("diseasename");

            _PostCard.Address = RequestTools.GetString("address");//病人详细地址
            if (_PostCard.Address.Trim().Length <= 0)
            {
                this.AddErrLine("您没有填写详细地址，我们将无法向您寄送明信片！！");
                return;
            }
            _PostCard.ZipCode = RequestTools.GetString("zip");
            if (_PostCard.ZipCode.Trim().Length <= 0)
            {
                this.AddErrLine("请填写正确的邮编！！");
                return;
            }

            _PostCard.Congratulation = RequestTools.GetString("congratulation");
            if (_PostCard.Congratulation.Trim().Length > 500)
            {
                this.AddErrLine("祝福语不得超过500个字符！");
                return;
            }

            bool succ = UserBLL.Instance.AddPostCardInfo(_PostCard);
            if (succ)
            {
                this.AddMsgLine("提交成功，谢谢您的参与,我们将在近期给您邮寄明信片！");
                string nextUrl = CookiesTools.GetPreUrl();
                this.SetUrl2(nextUrl);
                this.SetMetaRefresh(3, nextUrl);
            }
            else
            {
                this.AddMsgLine("系统错误！");
                return;
            }

        }
    }
}
